package com.example.front.serivce;

import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.common.exception.BusinessException;
import com.example.front.dto.WorkshopCreateDto;
import com.example.front.dto.WorkshopDto;
import com.example.front.dto.WorkshopListDto;
import com.example.front.dto.WorkshopSearchDto;
import com.example.front.entity.Factory;
import com.example.front.entity.Workshop;
import com.example.front.mapper.FactoryMapper;
import com.example.front.mapper.WorkshopMapper;
import com.example.interceptor.UserContext;
import com.example.passport.entity.User;
import com.example.passport.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FactoryService {

    @Resource
    FactoryMapper factoryMapper;

    @Resource
    UserService userService;

    @Resource
    WorkshopService workshopService;

    /**
     * 创建车间
     */
    public void createFactory(Factory factory) {
        User me = UserContext.getUser();

        if (factoryMapper.findByName(me.getTenantId(), factory.getName()) != null) {
            throw new BusinessException("名称已存在");
        }
        userService.findNameById(factory.getUserId());
        Factory factory1 = new Factory();
        BeanUtils.copyProperties(factory, factory1);
        factory1.setTenantId(me.getTenantId());
        factoryMapper.create(factory1);
    }

    /**
     * 列出车间
     */
    public ListDto<Factory> listFactory(Factory condition, Pagination pagination) {

        long total = factoryMapper.count(condition);
        if (total == 0) {
            return new ListDto<>(new ArrayList<>(), pagination);
        }

        pagination.setTotal(total);

        List<Factory> factoryList = factoryMapper.findAll(condition, pagination);

        List<Factory> factoryDtoList = factoryList.stream().map(factory -> {
            Factory factoryListDto = new Factory();
            BeanUtils.copyProperties(factory, factoryListDto);
            User user = userService.findUserById(factory.getUserId());
            if (user != null) {
                factoryListDto.setUserName(user.getName());
            } else {
                factoryListDto.setUserName("");
            }
            return factoryListDto;
        }).collect(Collectors.toList());

        return new ListDto(factoryDtoList, pagination);
    }

    /**
     * 跟新数据
     */
    public void update(@Valid Factory factory) {
        User me = UserContext.getUser();

        if (factoryMapper.findByName(me.getTenantId(), factory.getName()) != null && factoryMapper.findByIdName(me.getTenantId(), factory.getId(), factory.getName()) == null) {
            throw new BusinessException("名称已存在");
        }
        userService.findUserById(factory.getUserId());
        Factory factory1 = new Factory();
        BeanUtils.copyProperties(factory, factory1);
        factory1.setTenantId(me.getTenantId());
        if (factoryMapper.update(factory1) != 1) {
            throw new BusinessException("操作失败");
        }
    }

    /**
     * 删除
     */
    public void deleteById(long id, long tenantId) {
        Workshop workshop = workshopService.findById(id, tenantId);
        if (workshop != null && workshop.getId() != 0) {
            throw new BusinessException("该工厂下存在有车间，请将该工厂下的车间删除或修改再执行该操作");
        }
        if (factoryMapper.deleteById(id, tenantId) != 1) {
            throw new BusinessException("操作失败");
        }
    }

    /**
     * 查找车间
     */
    public Factory findById(long id, long tenantId) {
        Factory factory = factoryMapper.findById(id, tenantId);
        if (factory == null) {
            return Factory.builder().name("该工厂已被删除").build();
        }
        return factory;
    }
}
